//
//  SimpleCode.m
//  SimpleCode
//
//  Created by tusm on 16/8/12.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "SimpleCode.h"
#import <objc/runtime.h>
@implementation SimpleCode
+ (void)simpleCode:(Class)cls{
    // 固定字符串拼接
    NSMutableString *initHeadString = @"- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {\n\tif (self = [super init]) {".mutableCopy;
    NSMutableString *initTailString = @"   }\nreturn self;\n}".mutableCopy;
    // 获取内中, 所有属性的名字
    unsigned int count = 0;
    // 获取类中属性列表
    objc_property_t *propertes =  class_copyPropertyList(cls, &count);
    // 遍历列表, 取得名字
    for (int i = 0; i < count; i ++) {
        objc_property_t property = propertes[i];
        const char *propertyCString =  property_getName(property);
        NSString *propertyString = [NSString stringWithFormat:@"\t\t_%s = [aDecoder decodeObjectForKey:@\"%s\"];",propertyCString,propertyCString];
        [initHeadString appendString:propertyString];
        
        NSLog(@"%@",propertyString);
    }
    [initHeadString appendString:initTailString];
    NSLog(@"%@",initTailString);
    // searchPath中的枚举大部分是给OSX 使用的
    NSString *desktopPath = [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject];
    NSString *file = [desktopPath stringByAppendingPathComponent:@"SimpleFile"];
    NSData *data = [initHeadString dataUsingEncoding:NSUTF8StringEncoding];
    if ([data writeToFile:file atomically:YES]) {
        NSLog(@"创建成功");
    }else{
    
        NSLog(@"创建失败");
    }
 
//    NSLog(@"%s",propertyCString);
//    NSLog(@"%d",count);
}
@end
